package 第2节数组;

import java.util.Arrays;

//插入排序 O(n^2) S(1) 稳定排序
public class InsertionSort {
    public static void main(String[] args) {
        int[] arr = {5,3,1,7,4,8,2,6,};
        for (int i = 1; i < arr.length; i++) {
            for(int j = i;j > 0 && arr[j - 1] > arr[j] ;j--) {
                int temp = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = temp;
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}
